package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.MailingMassSendRequest;
import com.frogs.ucenter.v1.request.MailingRecieverAddRequest;
import com.frogs.ucenter.v1.request.MailingSendRequest;
import com.frogs.ucenter.v1.service.MailingService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION+"MailController")
@RequestMapping("/"+ApiConstants.VERSION)
public class MailingController extends BaseController3 {
	
	@Resource(name=ApiConstants.VERSION + "mailingService")
	private MailingService mailingService;
	
	private static final String MAIL_ROOT = "/mail";
	private static final String MAIL_SEND_TO_ONE = MAIL_ROOT + "/sendToOne";
	private static final String MAIL_SEND_TO_GROUPS = MAIL_ROOT + "/sendToGroups";
	private static final String MAIL_RECEIVER_ADD = MAIL_ROOT + "/reciever/add";
		
	/**
	 * 发送单条邮件
	 * @param request
	 * @return
	 */
	@RequestMapping(MAIL_SEND_TO_ONE)
	public Response mailSendToOne(HttpServletRequest request) {
		MailingSendRequest sendRequest = new MailingSendRequest(); 
		Response response;
		try {
			sendRequest.parse(request);
			response = mailingService.sendMail(sendRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 群发邮件
	 * @param request
	 * @return
	 */
	@RequestMapping(MAIL_SEND_TO_GROUPS)
	public Response mailSendToGroups(HttpServletRequest request) {
		MailingMassSendRequest sendRequest = new MailingMassSendRequest(); 
		Response response;
		try {
			sendRequest.parse(request);
			response = mailingService.sendMassMail(sendRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping({MAIL_RECEIVER_ADD})
	public Response mailRecieverAdd(HttpServletRequest request) {
		MailingRecieverAddRequest addRequest = new MailingRecieverAddRequest(); 
		Response response;
		try {
			addRequest.parse(request);
			response = mailingService.addReciever(addRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
}
